home *** CD-ROM | disk | FTP | other *** search
- /* Copyright 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* fonts.c - create display lists for an outline and a filled font
- * and use glCallLists() to display a string with them
- *
- * Left Mouse Button - change incidence and azimuth angles
- * Middle Mousebutton - change the twist angle based on
- * horizontal mouse movement
- * Right Mousebutton - zoom in and out based on vertical
- * mouse movement
- * <f> key - cycle thru outline/filled/stroke fonts
- * <R> Key - reset viewpoint
- * Escape key - exit the program
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
-
- #include "fonts.h" /* contains descriptions for 3 sample fonts */
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid mouse( GLint, GLint, GLint, GLint );
- GLvoid motion( GLint, GLint );
-
- GLuint create3DFont( GLfloat font[][1+MAX_STROKES*3] );
- GLvoid cycleFont( GLvoid );
-
- void resetView( GLvoid );
- void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLint action;
- static enum actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
-
- static GLint xStart = 0, yStart = 0;
- static GLfloat distance, twistAngle, incAngle, azimAngle;
-
- static GLuint outlineBase, filledBase, strokeBase, currentBase;
- static char string[] = "OpenGL Programming 1";
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( (width / 2) + 4, height / 4 );
- glutInitWindowSize( (width / 2) - 4, height / 2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutMouseFunc( mouse );
- glutMotionFunc( motion );
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- outlineBase = create3DFont( outlineFont );
- filledBase = create3DFont( filledFont );
- strokeBase = create3DFont( strokeFont );
-
- currentBase = filledBase;
-
- glutMainLoop();
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - render a string using fonts stored "
- "in display lists.\n"
- "Left Mousebutton - move eye position\n"
- "Middle Mousebutton - change twist angle\n"
- "Right Mousebutton - move up/down to zoom in/out\n"
- "<f> key - cycle through fonts\n"
- "<R> Key - reset viewpoint\n"
- "Escape key - exit the program\n\n",
- progname);
- }
-
- GLvoid
- resetView( GLvoid )
- {
- distance = 250.0;
- twistAngle = 0.0; /* rotation of viewing volume (camera) */
- incAngle = 0.0;
- azimAngle = 0.0;
- }
-
- GLvoid
- initgfx( void )
- {
- glClearColor( 0.0, 0.0, 0.0, 0.0 );
-
- resetView();
- }
-
- /* Create a display list for each letter in the font description
- * passed in.
- */
- GLuint
- create3DFont( GLfloat font[][1+MAX_STROKES*3] )
- {
- GLint mode, i, j;
- GLuint fontBase;
- GLfloat fontScale;
-
- fontBase = glGenLists( 256 );
- for (i = 0; font[i][0] != END_OF_LIST; i++) {
- glNewList( fontBase+(GLuint)font[i][0], GL_COMPILE );
- for (j = 1; mode = font[i][j]; j += 3) {
- if (mode == FONT_BEGIN) {
- fontScale = font[i][j+2];
- glBegin( (GLint) font[i][j+1] );
- } else if (mode == FONT_NEXT) {
- glVertex2f( font[i][j+1]*fontScale,
- font[i][j+2]*fontScale );
- } else if (mode == FONT_END) {
- glVertex2f( font[i][j+1]*fontScale,
- font[i][j+2]*fontScale );
- glEnd();
- } else if (mode == FONT_ADVANCE) {
- glTranslatef( font[i][j+1]*fontScale,
- font[i][j+2]*fontScale, 0.0 );
- }
- }
- glEndList();
- }
- return fontBase;
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport(0, 0, (GLint)width, (GLint)height);
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- aspect = (GLdouble) width / (GLdouble) height;
- gluPerspective( 45.0, aspect, 10.0, 500.0 );
- glMatrixMode(GL_MODELVIEW);
- }
-
- void
- polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
- GLfloat twist)
- {
- glTranslatef( 0.0, 0.0, -distance);
- glRotatef( -twist, 0.0, 0.0, 1.0);
- glRotatef( -incidence, 1.0, 0.0, 0.0);
- glRotatef( -azimuth, 0.0, 0.0, 1.0);
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'f': /* toggle filled mode */
- cycleFont();
- glutPostRedisplay();
- break;
- case 'R':
- resetView();
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- mouse( GLint button, GLint state, GLint x, GLint y )
- {
- static GLint buttons_down = 0;
-
- if (state == GLUT_DOWN) {
- switch (button) {
- case GLUT_LEFT_BUTTON:
- action = MOVE_EYE;
- break;
- case GLUT_MIDDLE_BUTTON:
- action = TWIST_EYE;
- break;
- case GLUT_RIGHT_BUTTON:
- action = ZOOM;
- break;
- }
-
- /* Update the saved mouse position */
- xStart = x;
- yStart = y;
- } else {
- if (--buttons_down == 0)
- action = MOVE_NONE;
- }
-
- }
-
- GLvoid
- motion( GLint x, GLint y )
- {
- switch (action) {
- case MOVE_EYE:
- /* Adjust the eye position based on the mouse position */
- azimAngle += (GLdouble) (x - xStart);
- incAngle -= (GLdouble) (y - yStart);
- break;
- case TWIST_EYE:
- /* Adjust the eye twist based on the mouse position */
- twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
- break;
- case ZOOM:
- /* Adjust the eye distance based on the mouse position */
- distance -= (GLdouble) (y - yStart)/10.0;
- break;
- default:
- printf("unknown action %d\n", action);
- }
-
- /* Update the stored mouse position for later use */
- xStart = x;
- yStart = y;
-
- glutPostRedisplay();
- }
-
- /* Define user input functions */
- GLvoid
- cycleFont( GLvoid )
- {
- if ( currentBase == outlineBase ) {
- currentBase = filledBase;
- } else if ( currentBase == filledBase ) {
- currentBase = strokeBase;
- } else {
- currentBase = outlineBase;
- }
- }
-
- GLvoid
- drawScene( GLvoid )
- {
-
- glClear( GL_COLOR_BUFFER_BIT );
-
- glColor3f( 1.0, 1.0, 1.0 );
- glPushMatrix();
- polarView( distance, azimAngle, incAngle, twistAngle );
-
- glPushMatrix();
- glTranslatef( -110.0, 0.0, 0.0 );
- /* save the current ListBase value, set it to
- * the start of the font and use the string itself
- * to index into the display lists
- */
- glPushAttrib(GL_LIST_BIT);
- glListBase( currentBase );
- glCallLists( strlen(string), GL_UNSIGNED_BYTE,
- (unsigned char *) string );
- glPopAttrib();
- glPopMatrix();
- glPopMatrix();
- glutSwapBuffers();
- }
-